package com.itita.ww2.handler.requesthandler.army;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.enums.PushCode;
import com.itita.ww2.model.user.DevTime.DevTimeType;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.DevKeyUtil;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 士兵生产完成
 * @author NorthLan
 *
 */
public class ArmyProduceComplete implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		//
		ISFSObject backObj = new SFSObject();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));
		//
		String key = params.getUtfString("devKey");
		String armyCode = DevKeyUtil.splitKey(key, 1);

		// 当前是否在生产
		if (!userProfile.getTimeManager().isDevTimeExists(DevTimeType.PRODUCE, armyCode)) {
			MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, WW2ExceptionCode.INVALID_OPT, false);
			return false;
		}

		// 时间是否已经达到
		if (!userProfile.getTimeManager().isTimeup(DevTimeType.PRODUCE, armyCode)) {
			MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, WW2ExceptionCode.INVALID_OPT, false);
			return false;
		}

		userProfile.getArmyManager().armyProduceComplete(armyCode);

		// 推消息
		ISFSObject pushObj = new SFSObject();
		pushObj.putUtfString("key", key);
		pushObj.putUtfString("code", armyCode);
		pushObj.putInt("count", userProfile.getArmyManager().getArmyCount(armyCode));
		MessageHandler.getInstance().pushGB(PushCode.GAME_UNIT_PRODUCE_COMPLETE, pushObj, user);
		//
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
